<?php
require_once "../vendor/autoload.php";
$config = require_once "./config.php";

$sdk = new \WeChatPay\WeChatSdk($config);
//扫码支付
$info = $sdk->nativePay('native20220315171122', 'Scarecrow测试商品', ['total'=>1], ['attach'=>'12345678987654321']);
var_dump($info);

//JSAPI支付
$openId = 'oUfEI0YgmswunUTYXZ50ElzDm7sg';
$info = $sdk->jsapiPay('native20220315171114', ['openid'=>$openId],'Scarecrow 小程序测试商品', ['total'=>1], ['attach'=>'987654321']);
var_dump($info);
$prepay_id = json_decode($info['data'], true)['prepay_id'];

//H5支付
$info = $sdk->h5Pay('native20220315171115','Scarecrow H5测试商品',['total'=>1],[
    'payer_client_ip'   =>  '14.108.212.144',
    'h5_info'           =>  [
        'type'          =>  'Wap',
        'app_name'      =>  '稻草人的应用',
        'app_url'       =>  'http://www.scarecrow.top'
    ]
], ['attach'=>'987654321']);
var_dump($info);

//APP支付
$info = $sdk->appPay('native20220315171117', 'Scarecrow APP测试商品', ['total'=>1], ['attach'=>'987654321']);
var_dump($info);

//微信支付订单号查询
$info = $sdk->transactionIdQuery('4200001314202203158581938526');
var_dump($info);

//商户订单号查询
$info = $sdk->outTradeNoQuery('native20220315171111');
var_dump($info);

//关闭订单API
$info = $sdk->outTradeNoClose('native20220315171107');
var_dump($info);

//申请退款API 微信支付订单号
$transactionId = '4200001303202203171607847522';
$outRefundNo = 'TK20220317170001';
$info = $sdk->transactionIdRefund($transactionId, $outRefundNo, ['refund'=>1, 'total'=>1, 'currency'=>'CNY'], ['reason'=>'Scarecrow 测试退款']);
var_dump($info);

//申请退款API 商户订单号
$outRefundNo = '20220317170015';
$info = $sdk->outTradeNoRefund('native20220315171122',$outRefundNo, ['refund'=>1, 'total'=>1, 'currency'=>'CNY'], ['reason'=>'Scarecrow 测试退款']);
var_dump($info);

//查询单笔退款AP
$outRefundNo = 'TK20220317170001';
$info = $sdk->OutRefundNoRefundQuery($outRefundNo);
var_dump($info);

//申请下载账单
$info = $sdk->tradeBill("2022-03-17");
if ($info['code'] == WX_RETURN_SUCCESS) {
    $downloadFile = json_decode($info['data'], true)['download_url'];
    $info = $sdk->downloadBill($downloadFile);
    if ($info['code'] == WX_RETURN_SUCCESS) {
        file_put_contents('./bill.csv', $info['data']);
    }
    var_dump("Download success.");
} else {
    var_dump("Download fail." . $info['msg']);
}

//申请资金账单下载
$info = $sdk->fundFlowBill("2022-03-17");
if ($info['code'] == WX_RETURN_SUCCESS) {
    $downloadFile = json_decode($info['data'], true)['download_url'];
    $info = $sdk->downloadBill($downloadFile);
    if ($info['code'] == WX_RETURN_SUCCESS) {
        file_put_contents('./bill.csv', $info['data']);
    }
    var_dump("Download success.");
} else {
    var_dump("Download fail." . $info['msg']);
}

//小程序调起签名
$prepay_id = 'wx21122808210882415bfc9d32b7f47d0000';
$sign = $sdk->clientOpenWxSign($prepay_id);
var_dump($sign);

//异步回调签名验证
$info = $sdk->notifyVerify('12312', '3123123','461892B4971829C59C1A7CA44F4064B771532E5F', '1647849555', '312313123');
var_dump($info);

